package com.love.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.cloud.admin.api.entity.SysAttentionApp;
import com.love.cloud.admin.api.entity.SysMenu;
import com.love.cloud.admin.mapper.SysAttentionAppMapper;
import com.love.cloud.admin.service.SysAttentionAppService;
import com.love.cloud.common.core.exception.BusinessException;
import com.love.cloud.common.security.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hongdongqu
 */
@Service
public class SysAttentionAppServiceImpl extends ServiceImpl<SysAttentionAppMapper, SysAttentionApp> implements SysAttentionAppService {

    @Resource
    SysAttentionAppMapper sysAttentionAppMapper;


    @Override
    public List<SysMenu> getListByUserId(String userId) {
        if (StringUtils.isEmpty(userId)) {
            throw new BusinessException("用户id不能为空");
        }
        List<SysMenu> listByUserId = this.baseMapper.getListByUserId(userId);
        return listByUserId;
    }
    @Override
    public Object collection(SysAttentionApp sysAttentionApp) {
        sysAttentionApp.setUserId(SecurityUtils.getUser().getId());
        QueryWrapper<SysAttentionApp> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysAttentionApp::getMenuId,sysAttentionApp.getMenuId())
                .eq(SysAttentionApp::getUserId, sysAttentionApp.getUserId());
        SysAttentionApp obj = sysAttentionAppMapper.selectOne(queryWrapper);
        if(obj==null){
            this.save(sysAttentionApp);
        }else {
            obj.setEnable(sysAttentionApp.getEnable());
            this.updateById(obj);
        }
        return true;
    }
}
